    //
//  FullScreenMegafaunaViewController.m
//  Digital Seamonsters
//
//  Created by Adam Cue on 2/22/11.
//  Copyright Adam Cue 2011.
//
//  This program is licensed and distributed under the terms of the GNU General Public License.  
//	The text for the license can be found in the license.txt file in this project.
//

#import "FullScreenMegafaunaViewController.h"

@interface FullScreenMegafaunaViewController (Private)
#pragma mark HelperMethods
- (void)longPress:(UIGestureRecognizer *)gestureRecognizer;
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
@end


@implementation FullScreenMegafaunaViewController

@synthesize fullImageView = _fullImageView;
@synthesize captionView = _captionView;
@synthesize name = _name;
@synthesize delegate;

- (void)viewDidLoad {
   [super viewDidLoad];
	self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
	_fullScrollView.contentSize = self.fullImageView.frame.size;
	[_fullScrollView setMaximumZoomScale:2.0];
   UILongPressGestureRecognizer * longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
   [_fullScrollView addGestureRecognizer:longPressGesture];
   [longPressGesture release];
   [UIView animateWithDuration:0.15 delay:0.0 options:UIViewAnimationCurveLinear animations:^{self.captionView.alpha = 1.0;} completion:nil];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
   return interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}

- (void)dealloc {
   self.name = nil;
   self.delegate = nil;
   
   [super dealloc];
}

#pragma mark HelperMethods
- (void)longPress:(UIGestureRecognizer *)gestureRecognizer{
   if(gestureRecognizer.state == UIGestureRecognizerStateBegan){
      UIActionSheet * actionSheet = [[[UIActionSheet alloc] initWithTitle:nil 
                                                                 delegate:self 
                                                        cancelButtonTitle:@"Cancel" 
                                                   destructiveButtonTitle:nil 
                                                        otherButtonTitles:@"Email Photo", @"Save Image", @"Copy", nil] autorelease];
      CGPoint touchLoc = [gestureRecognizer locationInView:self.view];
      [actionSheet showFromRect:CGRectMake(touchLoc.x, touchLoc.y, 1, 1) inView:self.view animated:YES];
   }
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
   if(error != NULL){
      UIAlertView * errorAlert = [[[UIAlertView alloc] initWithTitle:@"Error!" 
                                                             message:[NSString stringWithFormat:@"%@", error] 
                                                            delegate:nil cancelButtonTitle:@"OK"
                                                   otherButtonTitles:nil] autorelease];
      [errorAlert show];
   }
}

#pragma mark ExternalMethods
- (IBAction)close:(id)sender{
   [UIView animateWithDuration:0.15 delay:0.0 options:UIViewAnimationCurveLinear 
                    animations:^{
                       ((UIView *)sender).alpha = 0.0;
                       self.captionView.alpha = 0.0;
                    } completion:^(BOOL finished){
                       [self.delegate closeButtonPressed];
                    }];
}

#pragma mark UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
	return self.fullImageView;
}

#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
   if(buttonIndex == 0){
      MFMailComposeViewController * vc = [[MFMailComposeViewController alloc] init];
      vc.mailComposeDelegate = self;
      [vc setSubject:[NSString stringWithFormat:@"Incredible %@ photo", self.name]];
      [vc addAttachmentData:UIImageJPEGRepresentation(self.fullImageView.image, 1.0) 
                   mimeType:@"image/jpeg" 
                   fileName:[NSString stringWithFormat:@"Cachalot_%@_photo", self.name]];
      [self presentModalViewController:vc animated:YES];
   }
   else if(buttonIndex == 1){
      UIImageWriteToSavedPhotosAlbum(self.fullImageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
   }
   else if(buttonIndex == 2){
      UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
      [pasteboard setData:UIImageJPEGRepresentation(self.fullImageView.image, 1.0) forPasteboardType:@"public.jpeg"];
   }
}

#pragma mark MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
   [self dismissModalViewControllerAnimated:YES];
}

@end
